<?php
/**
 * Provides error pages
 */
class ErrorController extends BRail_Application_Controller_Abstract
{
    /**
     * Manage errors : 404, 403, 500
     *
     * If you want to deny access to a page, just throw a new Exception with 403 code
     * <code>
     * throw new Exception('You shall now pass !', 403);
     * </code>
     *
     * @return void
     */
    public function errorAction()
    {
        $errors = $this->_getParam('error_handler');
        if (!$errors) {
            $errors = (object) array('type' => Zend_Controller_Plugin_ErrorHandler::EXCEPTION_OTHER,
                'exception' => new Exception(), 'request' => $this->getRequest());
        }

        switch ($errors->type) {
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
                $this->getResponse()->setHttpResponseCode(404);
                $this->view->message = 'Page not found';
                break;
            default:
                if ($errors->exception->getCode() == 403) {
                    return $this->_forward('unauthorized');
                } else {
                    // Unexpected error. Let's log it
                    Application::getLogger()->err($errors->exception->getMessage());
                    $this->getResponse()->setHttpResponseCode(500);
                    $this->view->message = 'Application error';
                }
                break;
        }

        $this->view->exception = $errors->exception;
        $this->view->request = $errors->request;
    }

     /**
     * Manage 403 errors
     *
     * you should not call this function directly but use errorAction instead
     *
     * @return void
     */
    public function unauthorizedAction()
    {
        $this->getResponse()->setHttpResponseCode(403);
    }

    /**
     * Manage session expired error page
     *
     * you should not call this function directly but use errorAction instead
     *
     * @return void
     */
    public function sessionAction()
    {
        $session = new Zend_Session_Namespace('session');
        $this->view->authenticationToken = $session->authenticationToken;
        $this->_helper->layout->setLayout('error');
        $this->getResponse()->setHttpResponseCode(403);
    }
}